/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jzoomer;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 * @author COMPUTER
 */
public class panel extends JPanel implements MouseMotionListener, MouseListener{
    Rectangle rect;
    Toolkit kit;
    Dimension d;
    Robot robot;
    BufferedImage image;
    Image zoomimage;
    int mousex = 100, mousey = 100;
    int zoomsize = 50;
    int glasssize = 100;
    frame f;
    int oldx = 0;
    int oldy = 0;
    public panel(frame f){
        this.f =  f;
        addMouseMotionListener(this);
        addMouseListener(this);
        kit = Toolkit.getDefaultToolkit();
        d = kit.getScreenSize(); 
        rect = new Rectangle(d);        
        try {
            robot = new Robot();            
            image = robot.createScreenCapture(rect);
            image.flush();         
        } catch (AWTException ex) {
                Logger.getLogger(panel.class.getName()).log(Level.SEVERE, null, ex);
        }
        zoomimage = image.getSubimage(mousex, mousey, 200, 200);
    }
    @Override
    public void paintComponent(Graphics g) {        
//        g.drawImage(image, 0, 0, d.width, d.height, this);
        g.clearRect(0, 0, f.getWidth(), f.getHeight());
        g.setColor(Color.BLACK);
        g.drawString("Left Click = Zoom In", mousex - 110, mousey + 10);
        g.drawString("Right Click = Increase Size", mousex + 10, mousey + 10);
        g.drawString("Escape = Exit", mousex - 20, mousey + 30);
//        g.fillOval(mousex - 50, mousey -50, 100, 100);
        Graphics2D g2d = (Graphics2D)g;
//        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
//                                           RenderingHints.VALUE_ANTIALIAS_ON);
        Ellipse2D oval = new Ellipse2D.Double(f.getX() - 40, f.getY() - 40, 100, 100);
        g2d.setClip(oval);
//          Image im;
//          im = g2d.getClip();
          g.setClip(WIDTH, WIDTH, WIDTH, WIDTH);
        if(mousex - 100 < 0)
            mousex = 100;            
        if(mousey - 100 < 0)
            mousey = 100;
//        robot.mouseMove(mousex, mousey);   
        try{
//        zoomimage = image.getSubimage(mousex - glasssize - 50, mousey - glasssize - 50, glasssize + 100, glasssize + 100);
        zoomimage = image.getSubimage(mousex - 100, mousey - 100, 100, 100);
        }                
        catch(Exception e){
//            mousex = -200;
//            mousey = -200;
        }
        g2d.drawImage(zoomimage, mousex -  150, mousey - 150, 200, 200, this);
//        g2d.drawImage(zoomimage, mousex -  glasssize + 25, mousey - glasssize + 25, glasssize + 150, glasssize + 150, this);
//        g.setColor(Color.BLACK);
//        g.fillOval(mousex - 100, mousey - 100, 100, 100);
        
    }  
    @Override
    public void mouseDragged(MouseEvent e) {
        image = robot.createScreenCapture(rect);
        f.setLocation(e.getXOnScreen() - oldx, e.getYOnScreen() - oldy);
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        mousex = e.getX();
        mousey = e.getY();             
        repaint();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if(e.getButton() == MouseEvent.BUTTON3)
            glasssize += 50;
            if(glasssize > 200)
                glasssize = 100;            
    }

    @Override
    public void mousePressed(MouseEvent e) {
        oldx = e.getX();
        oldy = e.getY();
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        
    }

    @Override
    public void mouseExited(MouseEvent e) {
        
    }
}
